        package com.ruoyi.web.controller.system;

        import com.ruoyi.common.core.controller.BaseController;
        import com.ruoyi.common.core.domain.AjaxResult;
        import com.ruoyi.common.core.domain.entity.ServiceReservation;
        import com.ruoyi.common.core.page.TableDataInfo;
        import com.ruoyi.system.service.IServiceReservationService;
        import com.ruoyi.system.service.IServiceTypeService;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.security.access.prepost.PreAuthorize;
        import org.springframework.web.bind.annotation.*;

        import java.util.List;

        @RestController
        @RequestMapping("/system/reservation")
        public class ServiceReservationController extends BaseController {

            @Autowired
            private IServiceReservationService reservationService;
            @Autowired
            private IServiceTypeService serviceTypeService; //

        //    @PreAuthorize("@ss.hasPermi('system:reservation:list')")
            @GetMapping("/list")
            public TableDataInfo list(ServiceReservation reservation) {
                startPage();
                List<ServiceReservation> list = reservationService.selectServiceReservationList(reservation);
                return getDataTable(list);
            }

        //    @PreAuthorize("@ss.hasPermi('system:reservation:query')")
            @GetMapping(value = "/{id}")
            public AjaxResult getInfo(@PathVariable("id") Long id) {
                return AjaxResult.success(reservationService.selectServiceReservationById(id));
            }

        //    @PreAuthorize("@ss.hasPermi('system:reservation:add')")
    //    @PostMapping
    //    public AjaxResult add(@RequestBody ServiceReservation reservation) {
    //        // 这里手动把 serviceType 的 id 赋给 serviceTypeId
    //        if (reservation.getServiceType() != null) {
    //            reservation.setServiceTypeId(reservation.getServiceType().getId());
    //        }
    //        return reservationService.insertServiceReservation(reservation);
    //    }
        @PostMapping
        public AjaxResult add(@RequestBody ServiceReservation reservation) {
            Long serviceTypeId = null;
            if (reservation.getServiceType() != null) {
                serviceTypeId = reservation.getServiceType().getId();
                boolean exists = serviceTypeService.existsById(serviceTypeId);
                if (!exists) {
                    return AjaxResult.error("服务类型不存在，无法预约");
                }
                // 保存 ID 到数据库字段
                reservation.setServiceTypeId(serviceTypeId);
            } else {
                return AjaxResult.error("服务类型不能为空");
            }

            return reservationService.insertServiceReservation(reservation);
        }



            //    @PreAuthorize("@ss.hasPermi('system:reservation:edit')")
            @PutMapping
            public AjaxResult edit(@RequestBody ServiceReservation reservation) {
                return reservationService.updateServiceReservation(reservation);
            }

        //    @PreAuthorize("@ss.hasPermi('system:reservation:cancel')")
            @PutMapping("/cancel/{id}")
            public AjaxResult cancel(@PathVariable("id") Long id, @RequestParam("reason") String reason) {
                return reservationService.cancelReservation(id, reason);
            }

        //    @PreAuthorize("@ss.hasPermi('system:reservation:remove')")
            @DeleteMapping("/{ids}")
            public AjaxResult remove(@PathVariable Long[] ids) {
                return toAjax(reservationService.deleteServiceReservationByIds(ids));
            }

            @GetMapping("/availableTimeSlots")
            public AjaxResult getAvailableTimeSlots(@RequestParam("providerId") Long providerId,
                                                    @RequestParam("date") String date) {
                return AjaxResult.success(reservationService.getAvailableTimeSlots(providerId, date));
            }
            
            /**
             * 更新预约状态
             */
            @PutMapping("/{id}/status")
            public AjaxResult updateStatus(@PathVariable("id") Long id, @RequestBody ServiceReservation reservation) {
                return reservationService.updateReservationStatus(id, reservation.getStatus());
            }
        }